import { addAllToArray } from "@angular/core/src/render3/util";

export class DiskInfo {
    fileSystem: string;
    freeSpace: number;
    freeSpaceDetails: string;
    name: string;
    size: number;
    sizeDetails: string;
    percentage: number;
    percentageStyle: any;
    path: string;
    volumeName: string;
    displayName: string;
    /**
     * 设置磁盘名称
     * @param name 磁盘名称
     */
    setName(name: string) {
        if (name.endsWith(':')) {
            this.path = `${name}\\`;
            this.name = `${name}`;
        } else {
            this.path = `${name}:\\`;
            this.name = `${name}:`;
        }
    }
    setFreeSpace(freeSpace: number) {
        this.freeSpace = freeSpace;
        const gb = freeSpace / 1024 / 1024 / 1024;
        const mb = freeSpace / 1024 / 1024;
        const kb = freeSpace / 1024;
        if (Math.floor(gb) > 0) {
            this.freeSpaceDetails = `${Math.floor(gb)} GB`;
        } else if (Math.floor(mb) > 0) {
            this.freeSpaceDetails = `${Math.floor(mb)} MB`;
        } else if (Math.floor(kb) > 0) {
            this.freeSpaceDetails = `${Math.floor(kb)} KB`;
        } else {
            this.freeSpaceDetails = `${Math.floor(freeSpace)} 字节`;
        }
    }
    setSize(size: number) {
        this.size = size;
        const gb = size / 1024 / 1024 / 1024;
        const mb = size / 1024 / 1024;
        const kb = size / 1024;
        if (Math.floor(gb) > 0) {
            this.sizeDetails = `${Math.floor(gb)} GB`;
        } else if (Math.floor(mb) > 0) {
            this.sizeDetails = `${Math.floor(mb)} MB`;
        } else if (Math.floor(kb) > 0) {
            this.sizeDetails = `${Math.floor(kb)} KB`;
        } else {
            this.sizeDetails = `${Math.floor(size)} 字节`;
        }
    }
    refreshDetailInfo() {
        if (this.size > 0) {
            this.percentage = Math.floor(((this.size - this.freeSpace) / this.size) * 100);
            this.percentageStyle = {'width': `${this.percentage}%`};
        } else {
            this.percentage = 0;
            this.percentageStyle = {'width': '0%'};
        }
        if (this.fileSystem === 'NTFS' || this.fileSystem === 'FAT32') {
            this.displayName = `${this.volumeName && this.volumeName !== '' ?
            (`${this.volumeName}(${this.name})`) : `本地磁盘(${this.name})`}`;
        } else if (this.fileSystem === 'CDFS') {
            this.displayName = `BD-ROM驱动器(${this.name})`;
            if (this.volumeName && this.volumeName !== '') {
                this.displayName += this.volumeName;
            }
        }
    }
}
